/*	kd:
	
	Handles entry and filling in the inventory wheel for the quick select.
	
	*/

class Gcq_QuickSelectHandler : EventHandler {
	enum lists {
		spell_list = 0
	,	weapon_list
	,	item_list
	,	list_count
	}
	
	protected Gcq_QuickSelect			quick_select;
	protected Gcq_SpellSelect			spell_select;
	protected Gcq_WeaponSelect			weapon_select;
	protected Gcq_ItemSelect			item_select;
	
	protected Gcq_SelectableItems		item_lists [list_count];
	
	static Gcq_WeaponSelect SelectableWeapons () {
		let handler = Gcq_QuickSelectHandler(Find("Gcq_QuickSelectHandler"));
		
		if(handler) {
			return handler.weapon_select;
		}
		
		return NULL;
	}
	
	static Gcq_ItemSelect ItemSelect () {
		let handler = Gcq_QuickSelectHandler(Find("Gcq_QuickSelectHandler"));
		
		if(handler) {
			return handler.item_select;
		}
		
		return NULL;
	}

	override void WorldLoaded (WorldEvent event) {
		spell_select	= new("Gcq_SpellSelect");
		weapon_select	= new("Gcq_WeaponSelect");
		item_select		= new("Gcq_ItemSelect");
		
		item_lists [spell_list]		= spell_select;
		item_lists [weapon_list]	= weapon_select;
		item_lists [item_list]		= item_select;

		// Find the global list of all spells for us:
		let all_spells = Gc_SpellCollectionLoad.SpellPtr();

		spell_select.ChenEatsBreakfast(all_spells);
		weapon_select.TsDrawbridgesIsLiterallyStalingrad();
		item_select.THISISTHEULTIMATE();
		
		POLNAREFFASSISTMOTHERFUCKER();
		
		// kd: GIB QUICK SELECT
		quick_select = new("Gcq_QuickSelect");
		
		if(quick_select) {
			// console.printf(
				// "\c[ice]EVIL'S GOING UNDER DEADLY WHEEEEEEEEELS!");
			quick_select.OnBuild();
		}
	}
	
	protected void POLNAREFFASSISTMOTHERFUCKER () {
		let arti	= item_select.type_artefact;
		let inv		= item_select.type_inventory;
		
		item_select.Add("Medbag",			arti);
		item_select.Add("GCS_Stimpack",		arti);
		item_select.Add("GCS_Medipod",		arti);
		item_select.Add("GCS_Megahealth",	arti);
		item_select.Add("RejuvCaps",		arti);
		item_select.Add("HealPot",			arti);
		item_select.Add("RejuvPot",			arti);
		item_select.Add("IronLizardPot",	arti);
		item_select.Add("EnviroSuit",		arti);
		item_select.Add("WyvernIdol",		arti);
		item_select.Add("TiberiumBomb",		arti);
		item_select.Add("Propane",			arti);
		item_select.Add("TomeOfPowah",		arti);
		item_select.Add("Amplifier",		arti);
		item_select.Add("GunzerkPot",		arti);
		item_select.Add("BerserkPot",		arti);
		item_select.Add("Gunzerk",			arti);
		item_select.Add("Berserk2",			arti);
		item_select.Add("PhilosophersTome",	arti);
		item_select.Add("ManaShieldTome",	arti);
		item_select.Add("StridersLantern",	arti);
		item_select.Add("ArbiterModule",	arti);
		item_select.Add("VoidwalkersTouch",	arti);
		item_select.Add("PowersuitItem",	arti);
		item_select.Add("DraughTome",		arti);
		item_select.Add("DraughWithdraw",	arti);
		item_select.Add("DraughsuitItem",	arti);
		item_select.Add("IronblastRevert",	arti);
		item_select.Add("PowerAutoCannon",	arti);
		
		weapon_select.Ignore("LedgeGrabWeapon");
		weapon_select.Ignore("DynamiteTossWeapon");
		weapon_select.Ignore("ThrowObjectWeapon");
		
		weapon_select.Ignore("DraughsuitActivateWeapon");
		weapon_select.Ignore("DraughsuitDeactivateWeapon");
		weapon_select.Ignore("EnvirosuitWeapon");
		weapon_select.Ignore("CygborgActivateWeapon");
		weapon_select.Ignore("CygborgDeactivateWeapon");
	}

	override void WorldTick () {
		if(quick_select.IsActive()) {
			quick_select.Tick();
		}
	}
	
	// AHHHHHHH
	override bool InputProcess (InputEvent event) {
		if(quick_select.IsActive()) {
			quick_select.TickCursor(event.mousex, event.mousey);
		}
		
		return false;
	}

	override void RenderOverlay (RenderEvent event) {
		if(quick_select.IsActive()) {
			quick_select.Draw(event);
		}
	}
	
	protected bool is_open;

	protected void Open (int list_index, int who) const {
		if(list_index < 0 || list_count <= list_index) {
			return;
		}
		
		let item_list = item_lists [list_index];
		
		// kd:
		// If the actor cannot be cast to Guncaster, it's not a Guncaster for
		// this message, because if it's null, nobody is going to read it,
		// anyway.
		
		if(!Guncaster(players [who].mo) && consoleplayer == who) {
			Console.printf(
				"Your body is hexed! Change back "
				"before picking your inventory!");
			return;
		}
		
		if(!quick_select.IsActive() && item_list && who == consoleplayer) {
			let is_open = quick_select.OnOpen(
				players [who],
				item_list,
				list_index);
			
			if(is_open) {
				// SendNetworkEvent("DisableViewInterpol");
			}
			
			else {
				String msg;
				
				if(item_list == spell_select) {
					msg = "Can't select a spell if you don't have any!";
				}
				
				else if(item_list == weapon_select) {
					msg = "You have no weapons! What kinda world is this?";
				}
				
				else if(item_list == item_select) {
					msg = "You gotta find some items first!";
				}
				
				Console.printf(msg);
			}
		}
	}

	protected void Close (int who) const {
		if(quick_select.IsActive()) {
			players [who].mo.TakeInventory("NoSwayZone", 0x7fffffff);
			
			if(who == consoleplayer) {
				quick_select.Close();
			}
		}
	}
	
	protected void UseItem (int player, int list_index, int item) {
		let items = item_lists [list_index];
		
		if(items) {
			items.Use(players [player].mo, item);
		}
	}

	override void NetworkProcess (ConsoleEvent event) {
		let cmd	= event.name;
		let p	= event.player;

		if(gamestate == GS_TITLELEVEL) {
			return;
		}

		if(cmd ~== "huhuhumrsoup") {
			Open(spell_list, p);
		}
		
		else if(cmd ~== "leavenoenemystanding") {
			Open(WEAPON_LIST, p);
		}
		
		else if(cmd ~== "monsieurcandyshop") {
			Open(ITEM_LIST, p);
		}

		else if(cmd ~== "imatankengineer") {
			Close(p);
		}
		
		else if(cmd ~== "letemhaveitmac" && !event.ismanual) {
			UseItem(p, event.args [0], event.args [1]);
		}
	}
}
